package com.wxm.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xm.dao.LianXiDaoImpl;
import com.xm.dao.NewsDaoImpl;
import com.xm.dao.NewsTypeDaoImpl;
import com.xm.entity.LianXi;
import com.xm.entity.News;
import com.xm.entity.NewsType;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/NewsServlet")
public class NewsServlet extends BaseServlet {

	/**
	 * 处理新闻相关的请求
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toArticleListMore(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 接收客户端的参数
		String typeId = req.getParameter("typeId");
		// 新闻类型列表
		NewsDaoImpl moreDao = new NewsDaoImpl();
		ArrayList<News> moreList = moreDao.getAllList(typeId);
		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();
		session.setAttribute("com2", com2);

		req.setAttribute("moreList", moreList);
		req.getRequestDispatcher("article_list_more.jsp").forward(req, resp);
	}

	/**
	 * 新闻内容页面
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toArticleListContent(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 接收客户端的参数
		String id = req.getParameter("id");

		int ids = 0;
		if (id != null && !id.equals("")) {
			ids = Integer.valueOf(id);
		}
		// 调用dao层的方法，单查
		NewsDaoImpl moreDao = new NewsDaoImpl();
		News news = moreDao.getEmpById(ids);

		NewsTypeDaoImpl newsTypeDao = new NewsTypeDaoImpl();
		ArrayList<NewsType> newsType = newsTypeDao.getNewsType();
		req.setAttribute("newsType", newsType);

		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();
		session.setAttribute("com2", com2);

		// 跳转页面到查询
		req.setAttribute("news", news);
		req.getRequestDispatcher("article_list_content.jsp").forward(req, resp);
	}

	/**
	 * 获取新闻列表页面
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toArticleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 获取客户端传过来的参数
		String typeId = req.getParameter("typeId");
		// 调用dao层的方法
		NewsTypeDaoImpl newsTypeDao = new NewsTypeDaoImpl();
		ArrayList<NewsType> newsType = newsTypeDao.getNewsType();

		NewsDaoImpl moreDao = new NewsDaoImpl();
		ArrayList<News> newsList = moreDao.getListLimit6();

		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();
		session.setAttribute("com2", com2);

		// 跳转页面到查询
		req.setAttribute("newsType", newsType);
		req.setAttribute("newsList", newsList);
		req.getRequestDispatcher("article_list.jsp").forward(req, resp);
	}

	/**
	 * 获取新闻idת
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNewsByType(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收客户端传来的参数
		String typeId = req.getParameter("typeId");
		// 调用dao层的方法
		NewsTypeDaoImpl newsTypeDao = new NewsTypeDaoImpl();
		ArrayList<NewsType> newsType = newsTypeDao.getNewsType();

		NewsDaoImpl moreDao = new NewsDaoImpl();
		ArrayList<News> newsList = moreDao.getAllList(typeId);
		// 跳转页面到查询
		req.setAttribute("newsType", newsType);
		req.setAttribute("newsList", newsList);
		req.getRequestDispatcher("article_list.jsp").forward(req, resp);
	}

}
